/**
 * Created by Abby on 2017/4/2.
 */
//定义通过ID获取页面元素的方法
var $ = function(a) {
    return document.getElementById(a);
};

//定义通过Class获取页面元素的方法
var $list = function(z) {
    return document.getElementsByClassName(z);
};

//构建一个对象
var $C = function(tag) {
    return document.createElement(tag);
};

var globe_width = 0;
var globe_height = 0;

//窗口缩放
window.onresize = function() {
    //处理背景层
    //$('win_back').style.width = document.documentElement.clientWidth + 'px';
    //$('win_back').style.height = document.documentElement.clientHeight + 'px';

    //处理窗口居中的问题
   /* var win_inner = $('win_inner');
    win_inner.style.left = (document.documentElement.clientWidth - globe_width) / 2 + 'px';
    win_inner.style.top = (document.documentElement.clientHeight - globe_height) / 2 + 'px';*/
};


//定义打开窗口的方法
var win = function(winWidth, winHeight, title, cnt,c) {
    globe_width = winWidth;
    globe_height = winHeight;

    //用于关闭窗口
    var close = function() {
        _back.style.display = 'none';
        _inner.style.display = 'none';
        /*document.body.removeChild(_back);
        document.body.removeChild(_inner);*/
    };

    if (true) {
        //构造一个div(背景层)
        var _back = $C('div');
        _back.id = 'win_back';
        _back.style.position = 'absolute';
        _back.style.left = 0;
        _back.style.top = 0;
        _back.style.opacity = 0.8;
        _back.style.backgroundColor = '#d4e9ff';
        _back.style.width = document.documentElement.clientWidth + 'px';
        _back.style.height = document.documentElement.clientHeight + 'px';
        document.body.appendChild(_back);
    }//窗口背景

    if (true) {
        //构造窗口内容
        var _inner = $C('div');
        _inner.id = 'win_inner';
        _inner.style.position = 'absolute';
        _inner.setAttribute('win_width', winWidth);
        _inner.setAttribute('win_height', winHeight);
        /*_inner.win_width = winWidth;
         _inner.win_height = winHeight;*/
        _inner.style.left = (document.documentElement.clientWidth - winWidth) / 2 + 'px';
        _inner.style.top = (document.documentElement.clientHeight - winHeight) / 2 + 'px';
        _inner.style.width = winWidth + 'px';
        _inner.style.height = winHeight + 'px';
        _inner.style.border = '1px solid #3560ff';
        _inner.style.backgroundColor = 'white';
        document.body.appendChild(_inner);
        //窗口标题栏
        var _bar = $C('div');
        _bar.style.opacity = 0.8;
        _bar.style.backgroundColor = '#d4e9ff';
        _bar.style.width = winWidth + 'px';
        _bar.style.hegiht = '100px';
        _bar.style.border = '1px solid #3786a9';
        _inner.appendChild(_bar);
    }//窗口的标题

    if (true) {
        //标题和关闭按钮
        var _title = $C('div');
        _title.style.borderBottom = '1px solid #3560ff';
        _title.textContent = title;
        var _del = $C('span');
        _del.style.float = 'right';
        _del.style.cursor = 'pointer';
        _del.title = '关闭';
        _del.textContent = 'X';
        _del.onclick = function() {
            close();
        };
        _title.appendChild(_del);
        _bar.appendChild(_title);
    }//窗口的标题的关闭

    //窗口的内容
    cnt.style.display = 'block';
    _inner.appendChild(cnt);


    //...
    var add = function () {
        var name = document.getElementById('text2').value;
        //document.getElementById('text1').value = id;
        var sex = document.getElementById('select1').value;
        var remark = document.getElementById('text3').value;
        data.push({
            id:i++,
            name:name,
            sex:parseInt(sex),
            remark:remark
        });
    };
   /* var updata = function () {
        if (_row){
            var id = _row.getAttribute('_id');
             var name  = document.getElementById('text2').value;
             var sex  = document.getElementById('select1').value;
             var remark =document.getElementById('text3').value;
             for (var i = 0; i<data.length;i++){
             if (data[i].id === id - 0){
             data[i].name = name;
             data[i].sex = sex;
             data[i].remark = remark;
             break;
             }
             }
        }
    };*/
    if (true) {
        var _sub = $C('div');
        _sub.style.textAlign = 'center';
        _sub.style.marginTop = '10px';
        _inner.appendChild(_sub);

        var _btn1 = $C('button');
        _btn1.style.width = '80px';
        _btn1.style.height = '28px';
        _btn1.innerText = '保存';
        _btn1.onclick = function() {
                /*alert(document.getElementById('text1').value);*/
                if (document.getElementById('text1').value === "系统自动生成"){
                    add();
                    goPage(page);
                    close();
                }else{
                    var id = _row.getAttribute('_id');
                    for (var i = 0; i<data.length;i++){
                        if (data[i].id === id - 0){
                            data[i].name = document.getElementById('text2').value;
                            data[i].sex = document.getElementById('select1').value;
                            data[i].remark = document.getElementById('text3').value;
                            break;
                        }
                    }
                    goPage(page);
                    _row = 0;
                    close();
                }
        };
      _sub.appendChild(_btn1);
        /*
        var _update = $C('button');
        _update.style.width = '80px';
        _update.style.height = '28px';
        _update.innerText = '编辑';
        _update.onclick = function() {
            updata();
            grid(data);
            close();
        };
        _sub.appendChild(_update);*/

        var _btn2 = $C('button');
        _btn2.style.width = '80px';
        _btn2.style.height = '28px';
        _btn2.innerText = '关闭';
        _btn2.onclick = function() {
            close();
        };
        _sub.appendChild(_btn2);
    }//窗口的按钮

};